package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.BuyGuankanquanInfo;
import com.xgkj.diyiketang.util.LogUtils;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tao on 2017/6/29.
 */

public class WatchPageAdapter extends BaseAdapter {
    private Context mContext;
    private List<BuyGuankanquanInfo.ListBean.BuyDateDetailBean> mList = new ArrayList<>();
    private String quotaId;

    public WatchPageAdapter(Context context) {
        mContext = context;
    }
    public void updateData(List<BuyGuankanquanInfo.ListBean.BuyDateDetailBean> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }
    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mList!=null&&mList.size()>0?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_watch_page, null);
            childHolder.time = (TextView) convertView.findViewById(R.id.time);
            childHolder.state = (TextView) convertView.findViewById(R.id.state);
            childHolder.xiangxi = (TextView) convertView.findViewById(R.id.xiangxi);
            childHolder.jian_add = (TextView) convertView.findViewById(R.id.jian_add);
            childHolder.num = (TextView) convertView.findViewById(R.id.num);
            convertView.setTag(childHolder);
        } else {
            childHolder = (WatchPageAdapter.ChildHolder) convertView.getTag();
        }
        BuyGuankanquanInfo.ListBean.BuyDateDetailBean info = mList.get(position);
        if (info!=null){
            LogUtils.d("ooo",info);
            if (info.quotaId!=null){
                quotaId = info.quotaId;
            }
            //转运
            if (quotaId.equals("2737f7a418bf41c998728e6a0f9f253d")){
                childHolder.time.setText(info.createDate);
                if (info.status!=null){
                    String status = info.status;
                    if (status.equals("0")){
                        if (info.price!=null&&info.quotaSum!=null&&info.Status2!=null&&info.realQuotaSum!=null){
                            int i = Integer.parseInt(info.quotaSum) - Integer.parseInt(info.realQuotaSum);
                            if (info.Status2.equals("1")){
                                childHolder.xiangxi.setText("以"+info.price+"元价格购买"+info.quotaSum+"个观看券，成功交易"+info.realQuotaSum
                                        +"个观看券交易失败"+i+"个观看券");
                                childHolder.jian_add.setText("观看券增加+"+info.realQuotaSum);
                            }else {
                                childHolder.xiangxi.setText("以"+info.price+"元价格购买"+info.quotaSum+"个观看券，成功交易"+info.realQuotaSum
                                        +"个观看券交易失败"+i+"个观看券");
                                childHolder.jian_add.setText("观看券增加+"+info.realQuotaSum);
                            }
                        }
                    }else if (status.equals("1")){
                        if (info.price!=null&&info.quotaSum!=null&&info.Status2!=null&&info.realQuotaSum!=null){
                            int i1 = Integer.parseInt(info.quotaSum) - Integer.parseInt(info.realQuotaSum);
                            if (info.Status2.equals("1")){
                                childHolder.xiangxi.setText("以"+info.price+"元价格购买"+info.quotaSum+"个观看券，成功交易"+info.realQuotaSum
                                        +"个观看券交易失败"+i1+"个观看券");
                                childHolder.jian_add.setText("观看券减少+"+info.realQuotaSum);
                            }else {
                                childHolder.xiangxi.setText("以"+info.price+"元价格购买"+info.quotaSum+"个观看券，成功交易"+info.realQuotaSum
                                        +"个观看券交易失败"+i1+"个观看券");
                                childHolder.jian_add.setText("观看券减少+"+info.realQuotaSum);
                            }
                        }
                    }

                }
            }else {
                childHolder.time.setText(info.createDate);
                if (info.status!=null){
                    String status = info.status;
                    if (status.equals("0")){
                        if (info.price!=null&&info.quotaSum!=null&&info.Status2!=null&&info.realQuotaSum!=null){
                            int i = Integer.parseInt(info.quotaSum) - Integer.parseInt(info.realQuotaSum);
                            if (info.Status2.equals("1")){
                                childHolder.xiangxi.setText("以"+info.price+"元价格购买"+info.quotaSum+"个观看券，成功交易"+info.realQuotaSum
                                +"个观看券交易失败"+i+"个观看券");
                                childHolder.jian_add.setText("观看券增加+"+info.realQuotaSum);
                            }else {
                                childHolder.xiangxi.setText("以"+info.price+"元价格购买"+info.quotaSum+"个观看券，成功交易"+info.realQuotaSum
                                        +"个观看券交易失败"+i+"个观看券");
                                childHolder.jian_add.setText("观看券增加+"+info.realQuotaSum);
                            }
                        }
                    }else if (status.equals("1")){
                        if (info.price!=null&&info.quotaSum!=null&&info.Status2!=null&&info.realQuotaSum!=null){
                            int i1 = Integer.parseInt(info.quotaSum) - Integer.parseInt(info.realQuotaSum);
                            if (info.Status2.equals("1")){
                                childHolder.xiangxi.setText("以"+info.price+"元价格购买"+info.quotaSum+"个观看券，成功交易"+info.realQuotaSum
                                        +"个观看券交易失败"+i1+"个观看券");
                                childHolder.jian_add.setText("观看券减少+"+info.realQuotaSum);
                            }else {
                                childHolder.xiangxi.setText("以"+info.price+"元价格购买"+info.quotaSum+"个观看券，成功交易"+info.realQuotaSum
                                        +"个观看券交易失败"+i1+"个观看券");
                                childHolder.jian_add.setText("观看券减少+"+info.realQuotaSum);
                            }
                        }
                    }

                }
            }


        }
        return convertView;
    }
    class ChildHolder {
        TextView time,state,xiangxi,jian_add,num;
    }
}
